
var clicked_down_callback = null;
var fixed = null;
var drawing = null;
var drawing_timeout = null;
var canvas_div;
var demon_steps = 250;
var demon_step = 5;

var invoke_text = "In the name of all that is wicked and vile, I call upon Satan, Dark Lord of the Infernal Depths!  O mighty Prince of Darkness, call forth for me from among your countless minions a powerful demon, one profane and unholy, that I may most swiftly and completely satisfy my many depraved and disgusting carnal desires.  Hail Satan!";
function on_resize() {
	if (null == fixed) {
		canvas.setAttribute('width', window.innerWidth * window.devicePixelRatio);
		canvas.setAttribute('height', window.innerHeight * window.devicePixelRatio);
		//var s = 1/window.devicePixelRatio;
		//ctx.scale(s, s);
		var cw = window.innerWidth / window.devicePixelRatio;
		var ch = window.innerHeight / window.devicePixelRatio;
		canvas.style.width = window.innerWidth + 'px';
		canvas.style.height = window.innerHeight + 'px';

	} else {
		canvas.width = fixed.w;
		canvas.height = fixed.h;
		document.body.style.backgroundColor = 'gray';
	}
	click_div.style.width = window.innerWidth + 'px';
	click_div.style.height = window.innerHeight + 'px';
	ctx.globalCompositeOperation = 'lighter';
	draw_demon();
}


function init() {
	//document.addEventListener("touchstart", touch_started, false);
	document.addEventListener("touchmove", touch_moved, false);
	//document.addEventListener("touchend", touch_ended, false);
	click_div = document.getElementById('click_div');
	click_div.style.backgroundColor = "rgba(0,0,0,0)";
	canvas_div = document.getElementById('canvas_div');
	status_div = document.getElementById('status_div');
	canvas_init();
	clicked_down_callback = did_click;
	click_div.addEventListener('mousedown', clicked_down, false);
	click_div.addEventListener('touchstart', touch_started, false);
	window.onresize = on_resize;
	on_resize();
}

function touch_started(e) {
	//debug_log('touch_started');
	e.preventDefault();
	if (e.touches.length < 1) {
		return;
	}
	var o = {
		clientX : e.touches[0].pageX,
		clientY : e.touches[0].pageY,
		preventDefault: (function () { })
	};
	clicked_down(o);
}

function touch_moved(e) { 
	e.preventDefault();
}

function clicked_down(e) {
	e.preventDefault();
	if (null == clicked_down_callback) { return; }
	clicked_down_callback(e);
}

function did_click(e) {
	var hx = 1 - 2 * e.clientX / window.innerWidth;
	var hy = 1 - 2 * e.clientY / window.innerHeight;
	do_click(-hx, -hy);
}

function do_click(x, y) {
	draw_demon();
}

function draw_demon() {
	status_div.innerHTML = '&nbsp;';
	//canvas_div.style.visibility = "hidden";
	canvas_div.style.opacity = .5;
	drawing = demon_steps;
	canvas_clear();
	setTimeout(next_drawing, 500);
}

function update_demon_status(n) {
	if (.25 > Math.random()) {
		status_div.style.opacity = .5 * Math.random() + .5;
	}
	var extra = 1.5
	var p = Math.floor(n * invoke_text.length * extra);
	if (p > invoke_text.length) { 
		p = invoke_text.length; 
		var left = (n * extra - 1)/ (extra - 1)
		canvas_div.style.opacity = left;
		//canvas_div.style.visibility = "visible"
	}
	status_div.innerHTML = invoke_text.substr(0, p);
}

function next_drawing() {
	if (--drawing < 0) { 
		canvas_div.style.visibility = "visible";
		canvas_div.style.opacity = 1;
		return; 
	}
	update_demon_status((demon_steps - drawing) / demon_steps);
	var i = demon_step;
	while (--i > 0) { draw_random_dot(); }
	setTimeout(next_drawing, 75);
}

function draw_random_dot() {
	var range = 50 + Math.random() * 200 * drawing / demon_steps;
	var gr = Math.floor(1 + range * Math.random());
	var gx = Math.floor((canvas.width - gr) * Math.random());
	var gy = Math.floor((canvas.height - gr) * Math.random());
	var cr = Math.random();
	var cg = Math.random();
	var cb = Math.random();
	var ca = .2 + Math.random() * .8 * drawing / demon_steps;
	var ca = ca * Math.random();
	var sx;

	if (.25 < Math.random()) { 
		cr = cg = cb; 
	} 
	while (.5 < Math.random()) {
		sx = Math.random();
		cr *= sx; cg *= sx; cb *= sx;
		sx = (1 - sx) * Math.random();
		cr += sx; cg += sx; cb += sx;
	}
	/*
	while (.5 < Math.random()) { 
		sx = Math.random();
		cr *= sx; cg *= sx; cb *= sx;
	}
	*/
	cr = Math.floor(cr * 255);
	cg = Math.floor(cg * 255);
	cb = Math.floor(cb * 255);
	draw_fixed_dot(gr, gx, gy, cr, cg, cb, ca);
	draw_fixed_dot(gr, canvas.width - gr - gx, gy, cr, cg, cb, ca);
}
function draw_fixed_dot(gr, gx, gy, cr, cg, cb, ca) {
	var d = ctx.getImageData(gx, gy, gr, gr);
	var i;
	var col = 0;
	var row = 0;
	var m = gr * gr * 4;
	var h = gr / 2;
	var dx;
	var dy;

	for (i = 0; i < m; i += 4) {
		++col;
		if (col >= gr) { ++row; col = 0; }

		// distance from center

		dx = h - row;
		dy = h - col;
		dx *= dx;
		dy *= dy;

		dx = Math.sqrt(dx + dy);

		if (dx > h) { continue; }

		dx = dx / h;
		dy = 1 - dx;

		dy *= ca;
		dx = 1 - dy;

		d.data[i] = Math.floor(cr * dy + dx * d.data[i]);
		d.data[i + 1] = 
			Math.floor(cg * dy + dx * d.data[i + 1]);
		d.data[i + 2] = 
			Math.floor(cb * dy + dx * d.data[i + 2]);
		d.data[i + 3] = 255;
	}
	var d = ctx.putImageData(d, gx, gy);
}

